#include "dialogchoix2emeimage.h"

DialogChoix2emeImage::DialogChoix2emeImage(QWidget *parent) :
    QDialog(parent)
{
    ui.setupUi(this);
}

DialogChoix2emeImage::DialogChoix2emeImage(QWidget *parent, int indexCourante, QList<Image> listeImages, typeAppel type) :
    QDialog(parent)
{
    bool bon;
    int i, nbItem=0;

    ui.setupUi(this);

    this->type = type;

    correspondance = (int *) malloc(listeImages.size()*sizeof(int));

    for(i=0;i<listeImages.size();i++)
    {
        bon = true;

        if(type == this->imageMemeTaille)
        {
            if(listeImages[i].estGris == true || listeImages[i].qImage.width() != listeImages[indexCourante].qImage.width() || listeImages[i].qImage.height() != listeImages[indexCourante].qImage.height())
            {
                bon = false;
            }
        }
        else if (type == this->imagePasMemeTaille)
        {
            if(listeImages[i].estGris == true || listeImages[i].qImage.width()*listeImages[i].qImage.height()-2 < listeImages[indexCourante].qImage.width()*listeImages[indexCourante].qImage.height())
            {
                bon = false;
            }
        }
        else
        {
            bon = false;
        }
        if(bon)
        {
            ui.comboBoxChoixImage->addItem(QString((listeImages[i].getNom()).c_str()));
            correspondance[nbItem]=i;
            nbItem++;
        }
    }

    connect(ui.buttonBoxOkAnnuler, SIGNAL(accepted()), this, SLOT(validation()));
}

DialogChoix2emeImage::~DialogChoix2emeImage()
{

}

void DialogChoix2emeImage::validation()
{
    switch(type)
    {
        case imageMemeTaille :
            emit retourDialogMemeTaille(correspondance[ui.comboBoxChoixImage->currentIndex()]);
            break;
        case imagePasMemeTaille :
            emit retourDialogPasMemeTaille(correspondance[ui.comboBoxChoixImage->currentIndex()]);
            break;
    }

    accept();
}
